package work.vcloud.baseinfo.service.impl.coursetable;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import work.vcloud.baseinfo.service.intfc.coursetable.BaseDataService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 课表基础数据
 */
@Service
public class BaseDataServiceImpl implements BaseDataService {

    /**
     * 构建Grade
     *
     * @param originGradeMap
     * @return
     */
    @Override
    public Map<String, Object> fillGrade(Map<String, Object> originGradeMap) {
        Map<String, Object> gradeMap = new HashMap<>();
        gradeMap.put("Kind", "Grade");
        gradeMap.put("ID", originGradeMap.get("id"));
        gradeMap.put("OtData", originGradeMap.get("id"));
        gradeMap.put("ParentName", null);
        gradeMap.put("Name", originGradeMap.get("name"));
        gradeMap.put("ShortName", null);

        //序号
        String sortStr = (String) originGradeMap.get("sort");
        if (!StringUtils.isEmpty(sortStr)) {
            sortStr = "S" + String.format("%04d", Integer.parseInt(sortStr));
            gradeMap.put("Sequence", sortStr);
        } else {
            gradeMap.put("Sequence", null);
        }
        gradeMap.put("Code", null);
        gradeMap.put("Capacity", 0);
        return gradeMap;
    }

    /**
     * 构建Class
     *
     * @param originClassMap
     * @return
     */
    @Override
    public Map<String, Object> fillClass(Map<String, Object> originClassMap) {
        Map<String, Object> classMap = new HashMap<>();

        classMap.put("Kind", "Squad");
        classMap.put("ID", originClassMap.get("id"));
        classMap.put("OtData", originClassMap.get("id"));
        classMap.put("ParentName", originClassMap.get("gradename"));
        classMap.put("Name", originClassMap.get("name"));
        classMap.put("ShortName", null);
        //序号
        String sortStr = (String) originClassMap.get("sort");
        if (!StringUtils.isEmpty(sortStr)) {
            sortStr = "S" + String.format("%04d", Integer.parseInt(sortStr));
            classMap.put("Sequence", sortStr);
        } else {
            classMap.put("Sequence", null);
        }
        classMap.put("Code", null);
        classMap.put("Capacity", 0);
        return classMap;
    }

    /**
     * 构建Course
     *
     * @param originCourseMap
     * @return
     */
    @Override
    public Map<String, Object> fillCourse(Map<String, Object> originCourseMap) {
        Map<String, Object> courseMap = new HashMap<>();

        courseMap.put("Kind", "Subject");
        courseMap.put("ID", originCourseMap.get("id"));
        courseMap.put("OtData", originCourseMap.get("id"));
        courseMap.put("ParentName", null);
        courseMap.put("Name", originCourseMap.get("name"));
        courseMap.put("ShortName", originCourseMap.get("name").toString().substring(0, 1));
        //序号
        String sortStr = (String) originCourseMap.get("sort");
        if (!StringUtils.isEmpty(sortStr)) {
            sortStr = "S" + String.format("%04d", Integer.parseInt(sortStr));
            courseMap.put("Sequence", sortStr);
        } else {
            courseMap.put("Sequence", null);
        }
        courseMap.put("Code", null);
        courseMap.put("Capacity", 0);
        return courseMap;
    }

    /**
     * 构建Teacher
     *
     * @param originTeacherMap
     * @return
     */
    @Override
    public Map<String, Object> fillTeacher(Map<String, Object> originTeacherMap) {
        Map<String, Object> teacherMap = new HashMap<>();

        teacherMap.put("Kind", "Teacher");
        teacherMap.put("ID", originTeacherMap.get("id"));
        teacherMap.put("OtData", originTeacherMap.get("id"));
        teacherMap.put("ParentName", null);
        String idCard = (String) originTeacherMap.get("sfzjh");
        teacherMap.put("Name", originTeacherMap.get("name") + "(" + idCard.substring(idCard.length() - 4, idCard.length()) + ")");
        teacherMap.put("ShortName", originTeacherMap.get("name"));
        //序号
        String sortStr = (String) originTeacherMap.get("sort");
        if (!StringUtils.isEmpty(sortStr)) {
            sortStr = "S" + String.format("%04d", Integer.parseInt(sortStr));
            teacherMap.put("Sequence", sortStr);
        } else {
            teacherMap.put("Sequence", null);
        }
        teacherMap.put("Code", null);
        teacherMap.put("Capacity", 0);
        return teacherMap;
    }


    @Override
    public List getGradeData(JSONArray baseDataList) {
        List list = baseDataList.stream().filter(m->"Grade".equals(((JSONObject)m).get("Kind"))).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<JSONObject> getClassData(JSONArray baseDataList) {
        List list = baseDataList.stream().filter(m->"Squad".equals(((JSONObject)m).get("Kind"))).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<JSONObject> getCourseData(JSONArray baseDataList) {
        List list = baseDataList.stream().filter(m->"Subject".equals(((JSONObject)m).get("Kind"))).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<JSONObject> getTeacherData(JSONArray baseDataList) {
        List list = baseDataList.stream().filter(m->"Teacher".equals(((JSONObject)m).get("Kind"))).collect(Collectors.toList());
        return list;
    }

}
